$(document).ready(function(){
    var mainMenuUl = $('.mainNav');
    var mainMenuWidth = mainMenuUl.width();
    var subMenu = $('.subNav');
    var subMenuUl = $('.subNav ul');
    var subMenuWidth = subMenu.width();
    var activeLink;
    var mainMenuLeftMargin = 430;
    var subMenuSidePadding = 20;
    var subMenuNewWidth = mainMenuWidth-2*subMenuSidePadding;
    var leftOffset;
    var mainContent = $('.pageContent');
    var hideMenuButton = $('.menuHide');
    var headerWrapper = $('.headerW');
    var loading	= $('.ac_loading');
    var footer = $('.footer');
    var cycleImagesTime = 10000;
    
    if(subMenuNewWidth<500){
        subMenuNewWidth = 500;
        mainMenuWidth = subMenuNewWidth+2*subMenuSidePadding;
    }
    
    subMenu.css('margin-left', 980-mainMenuWidth).width(subMenuNewWidth);
    mainContent.css('margin-left', 980-mainMenuWidth).width(subMenuNewWidth);
    
    var ua = navigator.userAgent.toLowerCase();
    var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
    if(isAndroid){
        $('.ac_overlay').hide();
    }
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
       $('.ac_overlay').hide();
    }
    
    setTimeout(cycleImages, cycleImagesTime );
    function cycleImages(){
        $('.imageRotate .right').click();
        setTimeout(cycleImages, cycleImagesTime );
    }
    
    function fixSubmenu(listId){
        activeLink = $('.mainNav li a.active');
        leftOffset = activeLink.offset().left-mainMenuUl.offset().left;
        subMenuUl.css('margin-left', leftOffset);
        subMenuUl.html($('#'+listId).html());
        if(!subMenu.is(":visible")){
            subMenu.slideDown(350);
        }
    }
    function showContent(urlToFetch){
        loading.show();
		urlToFetch = urlToFetch.replace('#', '');
 		console.log(urlToFetch);
        $.ajax({
            url: urlToFetch,
            dataType: 'jsonp',
            complete: function(data){
                loading.fadeOut();
                mainContent.html(data.responseText).slideDown(350);
                loading.fadeOut();
                return false;
            }
        })
        return false;
    }
    function hideAll(){
        headerWrapper.slideUp(500);
        subMenu.slideUp(350);
        mainContent.fadeOut(350);
        hideMenuButton.removeClass('up').addClass('down');
        mainMenuUl.find('a').removeClass('active');
    }
    function showAll(){
        headerWrapper.slideDown(500);
        hideMenuButton.removeClass('down').addClass('up');
    }
    function mainMenuClick(clickedLink){
        mainMenuUl.find('a').removeClass('active');
        clickedLink.addClass('active');
        var rel = clickedLink.attr('rel');
        if(rel.match(/.*submenu.*/)){
            fixSubmenu(rel);
            mainContent.fadeOut(350);
        }
        else{
            if(subMenu.is(":visible")){
                subMenu.slideUp(350);
            }
            showContent(clickedLink.attr('href'));
        }    
    }
    hideMenuButton.click(function(){
        if(headerWrapper.is(":visible")){
            hideAll();
        }
        else{
            showAll();
        }
    });
    
    mainMenuUl.find('a').click(function(){
        mainMenuClick($(this))
        return false;
    });
    
    subMenuUl.find('a').live('click', function() {
        subMenuUl.find('a').removeClass('active');
        $(this).addClass('active');
        showContent($(this).attr('href'));
        return false;
    });
    
    $('#selectLanguage').click(function() {
        $('#languageMenu').fadeIn(350);
        return false;
    });
    $('#closeLanguage').click(function() {
        $('#languageMenu').fadeOut(350);
        return false;
    });
    
});
